package com.kuaimacode.kframework.agent.controller.login;

import com.kuaimacode.kframework.agent.services.agent.SysAgentService;
import com.kuaimacode.kframework.agent.services.order.OrderService;
import com.kuaimacode.kframework.agent.services.user.SysAgentUserService;
import com.kuaimacode.kframework.agent.util.SessionUtil;
import com.kuaimacode.kframework.mybatis.models.agent.SysAgentUser;
import com.kuaimacode.kframework.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 用户登录
 **/
@Slf4j
@Controller
public class LoginController{

    @Autowired
    private SysAgentUserService agentUserService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private SysAgentService agentService;

	@RequestMapping(value = {"/login"}, method = RequestMethod.GET)
	public String login() {
		return "login";
	}

	@RequestMapping("/index")
	public String list(ModelMap model) {
        model.put("index", agentService.agentIndex(SessionUtil.getAgentId()));
        model.put("endTime", DateUtil.getNow(DateUtil.LONG_DATE_FORMAT));
        model.put("startTime", DateUtil.format(DateUtil.plus(DateUtil.DateEnum.MONTH, -1), DateUtil.LONG_DATE_FORMAT));
		return "index";
	}

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String login(
			@RequestParam(value = "username", required = true) String username,
			@RequestParam(value = "password", required = true) String password,ModelMap model) {
		Subject subject = SecurityUtils.getSubject();
		UsernamePasswordToken token = new UsernamePasswordToken(username, password);
		try {
			subject.login(token);
			//查询登陆用户
            SysAgentUser agentUser = agentUserService.findByUserName(username);
            if (StringUtils.isBlank(agentUser.getAgentId())) {
                throw new AuthenticationException("分站ID不能为空");
            }
            SessionUtil.putUserId(agentUser.getUserId());
            SessionUtil.putAgentId(agentUser.getAgentId());
			return "redirect:index";
		} catch (AuthenticationException e) {
		    log.error("用户登陆失败, username: {}", username, e);
			model.put("message", "用户名密码错误");
		} catch (Exception e) {
            model.put("message", "登陆失败");
            log.error("用户登陆失败, username: {}", username, e);
        }
		return "login";
	}

	@RequestMapping(value = {"/logout"}, method = RequestMethod.GET)
	public String logout() {
		Subject subject = SecurityUtils.getSubject();
		subject.logout();
		// url重定向
		return "redirect:login";
	}



}